package com.example.task;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.concurrent.ExecutorService;
import java.util.Date;
import java.text.SimpleDateFormat;

@Component
public class TimerTask {
    
    private final ExecutorService executorService;
    
    @Autowired  // 通过构造器注入线程池
    public TimerTask(ExecutorService executorService) {
        this.executorService = executorService;
    }
    
    public void startCountdown(int seconds) {
        executorService.submit(() -> {
            SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
            try {
                for (int i = seconds; i > 0; i--) {
                    System.out.println("[线程 " + Thread.currentThread().getName() + "] " + 
                                     "剩余: " + i + "秒 | 当前时间: " + sdf.format(new Date()));
                    Thread.sleep(1000); // 暂停1秒
                }
                System.out.println("倒计时结束！");
            } catch (InterruptedException e) {
                System.out.println("任务被中断");
            }
        });
    }
}